package cc.telecomdigital.tdstock.activity.groups.monitor.util;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import cc.telecomdigital.tdstock.CommonDefn;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.activity.groups.more.push.util.Data;
import cc.telecomdigital.tdstock.activity.groups.more.push.util.DataRead;

/**
 * 
 * Project：tdstock3
 * Package：cc.telecomdigital.tdstock.activity.groups.monitor.util
 * Class：MonitorUtil Description： Creater：hunter
 * [chengpanggang@126.com//hunter@mango.cc] CreateTime：Date：Nov 10, 2011
 * Time：11:28:22 AM Modifier ：hunter [chengpanggang@126.com//hunter@mango.cc]
 * Modify time： Date：Nov 10, 2011 Time：11:28:22 AM Modify Remarks：
 * 
 * @version
 * 
 */
public class MonitorUtil implements BookMarkInterface
{

	public static List<String[]>	items;
	// by hunter 2011-11-02
	public static String		BOOKMARK_SET_URL	= "http://%s/notification/setBookmark.do?account=%s&token=%s&item=%s&type=%s&view=gphone&version=1.4.2.3.0.1";
	public static String		BOOKMARK_GET_URL	= "http://%s/notification/getBookmark.do?account=%s&token=%s&view=gphone&version=1.4.2.3.0.1";
	// public static final String NOUPLOAD="NOUPLOAD";
	public static final String	UPLOADSUCCESS		= "UPLOADSUCCESS";
	public static final String	UPLOADFAILURE		= "UPLOADFAILURE";
	// add bookmark success
	public static final String	_ADD_SUCCESS		= "_ADD_SUCCESS";
	// bookmark full with
	public static final String	_IS_FULL			= "_IS_FULL";

	public static final String	_IS_PRESENCE		= "_IS_PRESENCE";

	public static final String	_IS_NET_ERROR		= "_IS_NET_ERROR";

	public static final String	_IS_NOT_FULL		= "_IS_NOT_FULL";
	/**
	 * this is class example
	 */
	private static MonitorUtil	monitor			= new MonitorUtil();

	public static final String	DEFAULTRESULT		= "DEFAULTRESULT";

	private static final String	TAG				= MonitorUtil.class
													.getSimpleName();

	private MonitorUtil()
	{
		// account=((ITDLApplication)getApplicationContext()).getSessionAccount();
		// token=((ITDLApplication)getApplicationContext()).getSessionToken();
		// webServer=((ITDLApplication)getApplicationContext()).getWebServer();
	}

	/**
	 * Instance has to do thread-safe
	 * 
	 * @return
	 */
	public static MonitorUtil getInstance()
	{
		return monitor;
	}

	public static String getBookMarkListURL(String webServerURL,
			String account, String token)
	{
		String temp = String.format(BOOKMARK_GET_URL, webServerURL, account,
				token);
		// String temp=String.format(PortfolioActivity.BOOKMARKS_LIST,
		// ((ITDLApplication)getApplicationContext()).getWebServer(),((ITDLApplication)getApplicationContext()).getSessionAccount(),((ITDLApplication)getApplicationContext()).getSessionToken());
		return temp;
	}

/*	// get items
	*//**
	 * 
	 * @return List<String[2]> --PortfolioActivity.items
	 *//*
	public List<String[]> getItems(Context context)
	{
		Data.getDomainAndAccountAndTokenAndDevice_token(context);
		List<String[]> items = ReadBookMark.getInstance()
				.getItems(getBookMarkListURL(Data.webServer, Data.account,
						Data.token), context);
		return items;
	}*/

	// get bookmark number
	/**
	 * @author hunter chengpanggang@126.com/hunter@mango.cc
	 * @return this is bookmark number
	 */
	public int getBookMarkNumber(List<String[]> itemlists)
	{
		// SharedPreferences
		// share=context.getSharedPreferences(CommonDefn.SYSTEM_CURRENT_SETTING,
		// Context.MODE_PRIVATE);
		// int num=share.getInt(CommonDefn.BOOKMARK_NUMBER, 0);
		// TDStockLog.d("test", "share------>"+num);
		// if(num==0){
		/*
		 * if(MonitorUtil.items==null){ // try{ MonitorUtil.items=getItems(
		 * context);
		 */
		// }catch(Exception e){
		// num=0;
		// }
		if (itemlists == null)
		{
			return -1;
		}
		TDStockLog.d(TAG, TAG+"----->"+ itemlists.size());
		return itemlists.size();
	}

	/**
	 * @author hunter chengpanggang@126.com/hunter@mango.cc
	 * @param items
	 *            this is remove items Stirng [] items.get(i)[0]=null
	 */
	/*
	 * public void updateItem(List<String[]> items) { int index = items.size() +
	 * 1; for (int i = 0; i < items.size(); i++) { if (items.get(i)[0] == null)
	 * { index = i; } } if (index > items.size()) { return; }
	 * items.remove(index); updateItem(items); }
	 */

	// get type(apdater) stringbuffer
	/**
	 * @param types
	 *            is itmes string[0]=itemnName,string[1]=apdter,string[2]=type
	 *            the is int type
	 * @author hunter chengpanggang@126.com/hunter@mango.cc
	 * @return
	 */
	public StringBuffer getType(List<String[]> types)
	{
		StringBuffer temp = null;
		for (int i = 0; i < types.size(); i++)
		{
			if (types.get(i)[0] != null)
			{
				if (temp == null)
				{
					temp = new StringBuffer(types.get(i)[1]);
				}
				else
				{
					temp.append(",").append(types.get(i)[1]);
				}
			}
		}
		TDStockLog.d(TAG, temp == null ? " type is null" : temp.toString());
		return temp;
	}

	// get stock Item stringbuffer
	public StringBuffer getItem(List<String[]> items)
	{
		StringBuffer temp = null;
		for (int i = 0; i < items.size(); i++)
		{
			if (items.get(i)[0] != null)
			{
				if (temp == null)
				{

					temp = new StringBuffer(items.get(i)[0]);
				}
				else
				{
					temp.append(",").append(items.get(i)[0]);
				}
			}
		}
		return temp;
	}

	// // adapter convert to int type
	// public int type(String adapter,String itemcode) {
	// int type = 0;
	// if
	// (StockInfo.ADAPTER.equalsIgnoreCase(adapter)&&TextUtils.isDigitsOnly(itemcode))
	// {
	// type = 1;
	// } else if
	// (IndexInfo.ADAPTER.equalsIgnoreCase(adapter)&&!TextUtils.isDigitsOnly(itemcode))
	// {
	// type = 2;
	// } else if (FuturesDelayInfo.ADAPTER.equalsIgnoreCase(adapter)) {
	// type = 3;
	// } else if (WorldIndexInfo.ADAPTER.equalsIgnoreCase(adapter)) {
	// type = 4;
	// } else if (WorldFuturesInfo.ADAPTER.equalsIgnoreCase(adapter)) {
	// type = 5;
	// } else if (ForeignExchangeInfo.ADAPTER.equalsIgnoreCase(adapter)) {
	// type = 6;
	// } else if (IntlMetalInfo.ADAPTER.equalsIgnoreCase(adapter)) {
	// type = 7;
	// } else if (OilInfo.ADAPTER.equalsIgnoreCase(adapter)) {
	// type = 8;
	// } else if (IndexFuturesInfo.ADAPTER.equalsIgnoreCase(adapter)) {
	// type = 9;
	// }
	// return type;
	//
	// }

	/**
	 * 
	 * @param stockCode
	 * @param type
	 * @return add sucess=true,add failre=false
	 */
	public String addBookMarkItem(List<String[]> items, String stockCode,
			int type, Context context)
	{
		if (items == null)
		{
			return _IS_NET_ERROR;
		}
		// updateItem(items);
		// Determine whether the full
		String result = isFull(items, context);
		if (result.equalsIgnoreCase(_IS_FULL))
		{
			return _IS_FULL;
		}
		else if (result.equalsIgnoreCase(_IS_NET_ERROR))
		{
			return _IS_NET_ERROR;
		}
		for (int i = 0; i < items.size(); i++)
		{
			if (items.get(i)[0].equals(stockCode)
					&& type == Integer.parseInt(items.get(i)[1]))
			{
				
				return _IS_PRESENCE;
			}
		}
		String[] temp = new String[2];
		temp[0] = stockCode;
		temp[1] = type + "";
		items.add(temp);
		MonitorUtil.items = items;
		return _ADD_SUCCESS;
	}

	// Monitoring to determine whether the table is full
	// full=true
	// not full =false
	/**
	 * @author hunter chengpanggang@126.com/hunter@mango.cc
	 * @param lists
	 *            List<String[]>
	 * @return true=full,not full=false
	 * 
	 */
	public String isFull(List<String[]> lists, Context context)
	{
		int current_bookmark_number = getBookMarkNumber(lists);
		// Exceed Max number
		TDStockLog.d(TAG,
				"lists.size------->" + (lists == null ? 0 : lists.size()));
		TDStockLog.d(TAG, "current_bookmark_number-------->"
				+ current_bookmark_number);
		if (current_bookmark_number == -1)
		{
			/*
			 * new CustomizeDialog(getMyOwnContext(context), "",
			 * context.getResources().getString(R.string.net_error)).show();
			 */
			return _IS_NET_ERROR;
		}
		if (current_bookmark_number >= CommonDefn.DEALRY_MAX_BOOKMARK_NUMBER)
		{
			/*
			 * new CustomizeDialog(getMyOwnContext(context), "",
			 * context.getResources
			 * ().getString(R.string.monitor_is_full_with_items)).show();
			 */
			return _IS_FULL;
		}
		if (lists.size() > CommonDefn.DEALRY_MAX_BOOKMARK_NUMBER)
		{
			/*
			 * new CustomizeDialog(getMyOwnContext(context), "",
			 * context.getResources
			 * ().getString(R.string.monitor_is_full_with_items)).show();
			 */
			return _IS_FULL;
		}
		return _IS_NOT_FULL;
	}

	public Context getMyOwnContext(Context context)
	{
		return ((Activity) context).getParent() == null ? context
				.getApplicationContext() : ((Activity) context).getParent();
	}

/*	// 非延迟模式下的上传监察表
	*//**
	 * @author hunter chengpanggang@126.com/hunter@mango.cc
	 * @return upload result
	 *//*
	public String upLoadBookMarksToServer(StringBuffer bookMarkItem,
			StringBuffer bookMarkType, Context context)
	{
		Data.getDomainAndAccountAndTokenAndDevice_token(context);
		String url = String.format(BOOKMARK_SET_URL, Data.webServer,
				Data.account, Data.token, bookMarkItem == null ? ""
						: bookMarkItem, bookMarkType == null ? ""
						: bookMarkType);
		// String
		// url=String.format(BOOKMARK_URL,"quotedev3.telecomdigital.cc",((ITDLApplication)getApplicationContext()).getSessionAccount(),((ITDLApplication)getApplicationContext()).getSessionToken(),bookMarkItem,bookMarkType);
		String result = "reslut";
		TDStockLog.d(TAG, url);
		// result =
		// ReadStatus.getInstance().getStatus(DataRead.getContentFromURL2(url));
		result = ReadStatus.getInstance().getStatus(
				DataRead.getContentFromURL(url));
		TDStockLog.d(TAG, result == null ? "" : result);
		// TDStockLog.d("test", "upLoadBookMarksToServer");
		if (result == null)
		{
			return UPLOADFAILURE + "&" + "result";
		}
		else
		{
			return UPLOADSUCCESS + "&" + result;
		}
		// }
	}*/

	// update bookMark number
	/*
	 * public void updateBookMarkNumber(Context context){ //SharedPreferences
	 * share=context.getSharedPreferences(CommonDefn.SYSTEM_CURRENT_SETTING,
	 * Context.MODE_PRIVATE); //SharedPreferences.Editor editor=share.edit();
	 * 
	 * MonitorUtil.items=getItems(context); updateItem(MonitorUtil.items); //int
	 * num=share.getInt(CommonDefn.BOOKMARK_NUMBER, 0);
	 * //if(num!=MonitorUtil.items.size()){
	 * //MonitorUtil.items=getItems(context); //num=MonitorUtil.items.size();
	 * //} //editor.putInt(CommonDefn.BOOKMARK_NUMBER, num); //editor.commit();
	 * }
	 */
	// public String adapter(int num) {
	// String value = "";
	// switch (num) {
	// case 1:
	// value = StockInfo.ADAPTER;
	// break;
	// case 2:
	// value = IndexInfo.ADAPTER;
	// break;
	// case 3:
	// value = FuturesDelayInfo.ADAPTER;
	// break;
	// case 4:
	// value = WorldIndexInfo.ADAPTER;
	// break;
	// case 5:
	// value = WorldFuturesInfo.ADAPTER;
	// break;
	// case 6:
	// value = ForeignExchangeInfo.ADAPTER;
	// break;
	// case 7:
	// value = IntlMetalInfo.ADAPTER;
	// break;
	// case 8:
	// value = OilInfo.ADAPTER;
	// break;
	// case 9:
	// value = IndexFuturesInfo.ADAPTER;
	// break;
	// }
	// return value;
	// }
	/**
	 * lists--net bmp--local
	 * 
	 * true---update,upload false---not update
	 * 
	 * @param lists
	 * @param bmp
	 * @return
	 */
	public static boolean isUpdated(List<String[]> lists, List<String[]> bmp)
	{
		if (lists == null)
		{
			return false;
		}
		if (bmp == null)
		{
			return false;
		}
		if (bmp.size() != lists.size())
		{
			return true;
		}
		if (isSameList(lists, bmp))
		{
			return false;
		}
		else
		{
			return true;
		}
	}

	/**
	 * 判断元素是否相同 true-same false-no same
	 * 
	 * @param lists
	 * @param bmp
	 * @return
	 */
	public static boolean isSameList(List<String[]> lists, List<String[]> bmp)
	{
		boolean flag = true;
		for (int i = 0; i < lists.size(); i++)
		{
			if (lists.get(i)[0].trim().equals(bmp.get(i)[0].trim()))
			{
			}
			else
			{
				flag = false;
			}
		}
		return flag;
	}
}
